﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace QualityRPT.Common
{
    public class Utility
    {
        public Utility()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 弹出提示框，提示指定信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="page">提示的页面</param>
        public static void Alert(string msg, System.Web.UI.Page page)
        {
            System.Web.UI.ClientScriptManager csm = page.ClientScript;
            System.Text.StringBuilder strScript = new System.Text.StringBuilder();
            string scriptKey = "no";
            if (!csm.IsClientScriptBlockRegistered(scriptKey))
            {
                strScript.Append("alert('");
                strScript.Append(msg);
                strScript.Append("');");
                csm.RegisterClientScriptBlock(page.GetType(), scriptKey, strScript.ToString(), true);
            }
        }

        /// <summary>
        /// 弹出提示框，并跳转
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="msgtype">跳转前页面</param>
        /// <param name="page">指定跳转的页面</param>
        public static void Alert(string msg, System.Web.UI.Page page, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());
        }
        /// <summary>
        /// 返回当前页面的URL的 "http://主机" 部分
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string Url(Page page)
        {
            Uri uri = page.Request.Url;
            string url = uri.OriginalString.Replace(uri.PathAndQuery, "");
            string appPath = page.Request.ApplicationPath;
            if (!string.IsNullOrEmpty(appPath))
                url += page.Request.ApplicationPath;

            return url;
        }
    }
}